package service

import (
	"encoding/json"
	"fmt"
	"usplatformserver/pkg/zmq"
)

type LicenseCodeInfo struct {
	LicenseCode string `json:"licenseCode" gorm:"Column:licenseCode"`
}

type getLicenseCodeReq struct {
	MsgType  uint32 `json:"msgType"`
	DeviceId string `json:"deviceId"`
}

func (svc Service) GetLicenseCode(addr, deviceId string) (string, error) {
	// 获取注册码消息请求
	msgReq := getLicenseCodeReq{
		MsgType:  zmq.MsgGetLicenseCode.MsgType,
		DeviceId: deviceId,
	}
	msgReqStr, err := json.Marshal(msgReq)
	if err != nil {
		fmt.Println("json.Marshal(msgReq) failed: ", err)
		return "", err
	}
	rcvMsg, err := zmq.SendMessage(addr, string(msgReqStr))
	var rcv LicenseCodeInfo
	err = json.Unmarshal([]byte(rcvMsg), &rcv)
	if err != nil {
		fmt.Println("json.Unmarshal failed: ", err)
		return "", err
	}
	return rcv.LicenseCode, nil
}
